<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>添加角色</title>
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<link rel="stylesheet" href="../../css/oksub.css">
	<script type="text/javascript" src="../../lib/loading/okLoading.js"></script>
</head>
<body>
<div class="ok-body">
	<!--form表单-->
	<!--<form class="layui-form layui-form-pane ok-form">-->
	<form class="layui-form ok-form" lay-filter="sysrole-add">
		<div class="layui-form-item">
			<label class="layui-form-label">角色名称</label>
			<div class="layui-input-block">
				<input type="text" name="roleName" placeholder="角色中文名称" autocomplete="off" class="layui-input" lay-verify="required" lay-verType="tips" id="roleName">
			</div>
		</div>

		<div class="layui-form-item">
			<label class="layui-form-label">角色别名</label>
			<div class="layui-input-block">
				<input type="text" name="roleCode" placeholder="角色英文简称" autocomplete="off" class="layui-input" lay-verify="required|roleCode" lay-verType="tips" id="roleCode">
			</div>
		</div>

		<div class="layui-form-item">
			<label class="layui-form-label">备注</label>
			<div class="layui-input-block">
				<input type="text" name="remark" placeholder="用户什么也没有留下" autocomplete="off" class="layui-input">
			</div>
		</div>

		<div class="layui-form-item">
			<div class="layui-input-block">
				<button class="layui-btn" lay-submit lay-filter="add">立即提交</button>
				<button type="reset" class="layui-btn layui-btn-primary">重置</button>
			</div>
		</div>
	</form>
</div>
<!--js逻辑-->
<script src="../../lib/layui/layui.js"></script>
<script src="../../libme/pages/body.js"></script>
<script src="../../libme/script/pinyin.js"></script>
<script type="text/javascript">
	layui.use(["element", "form", "tree", "okLayer", "okUtils", "jquery","okMock","api"], function () {
		let form = layui.form;
		let okLayer = layui.okLayer;
        let okMock = layui.okMock;
        let $ = layui.jquery;
        let api = layui.api;
		okLoading.close();

		// tree.render({
        // 	elem: "#permissionTree",
        // 	//data: okMock.api.permission.list,
        // 	data: data,//从本地 localStorage 中取值
        // 	showCheckbox: true
        // });

		form.verify({
            roleCode: function(value, item){ //value：表单的值、item：表单的DOM对象
                if(!new RegExp("^[a-zA-Z0-9_]+$").test(value)){
                    return '角色名不能有特殊字符';
                }
                if(/^\d+\d+\d$/.test(value)){
                    return '角色名不能全为数字';
                }
                if(!new RegExp("^[a-zA-Z0-9_]{4,24}$").test(value)){
                    return '角色名长度必须为4-24个字符';
                }
            }
		})
        form.on("submit(add)", function (data) {
			// TODO 权限节点校验
			api.request({
				url:okMock.api.role.create,
				where: data.field,
				dataType: api.jsonType.ujson
			},function (resp, stat) {
                api.greenTickMsg("添加成功", function () {
                    parent.layer.close(parent.layer.getFrameIndex(window.name));
                });
            })
			return false;
		});

        $(function () {//页面加载
			//
            $('input[name="roleName"]').keyup(function (){
                $('input[name="roleCode"]').val(pinyin.getCamelChars($('input[name="roleName"]').val()));
			});


			// 角色名称是否已经存在，进行数据库校验
			$('input[name="roleCode"]').blur(function () {
			    var ele = "#roleCode";
                var that = $(this)
				//layer.msg($(this).val())
				var val =  $(this).val().trim();
				if(val == '' || val.length <=0 || val == undefined) {
					api.tips('角色名不能为空',ele,'d');
					that.focus();
					return false;
                }
                if(!new RegExp("^[a-zA-Z0-9_]{4,24}$").test(val)) {
                    api.tips('角色名长度必须为4-24个字符',ele,'d');
                    that.focus();
                    return false;
                }
                if(/^\d+\d+\d$/.test(val)){
                    api.tips('角色名不能全为数字',ele,'d');
                    that.focus();
                    return false;
                }
                if(!new RegExp("^[a-zA-Z0-9_]+$").test(val)) {
                    api.tips('角色名不能有特殊字符',ele,'d');
                    that.focus();
                    return false;
                }

                $.ajax({
                    url: okMock.api.role.checkRoleCodeExist,
                    dataType: api.jsonType.text,
                    data: {roleCode: val},
					//sync: false,
                    success: function (resp, stat) {
                        console.log(resp)
                        if(resp=='true') {
                            //layer.msg("用户名已存在")
                            api.tips('角色名已经存在，请换一个角色名', ele, 'd')
                            that.focus()
                        }
                    }
                })
				return false
            })
        })
	})
</script>
</body>
</html>
